﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Azad;
using BCL.Entity;
using BCL.Repository;
using NazSoft.Framework;
using NazSoft.Framework.Extensions;
public partial class Admin_Commercials : BasePage //System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            BindCommercialList();


            string cid = Request.QueryString["cid"];
            if (String.IsNullOrEmpty(cid) == false)
            {
                ShowAddEditCommercial(cid);
            }
            SilverlightService ser = new SilverlightService();
            var menuCommercial = ser.GetCommercialMenu().Where(f => f != null).ToList();
            CommercialTypeDropDownList.DataSource = menuCommercial;
            CommercialTypeDropDownList.DataTextField = "Description";
            CommercialTypeDropDownList.DataValueField = "LookUpID";

            CommercialTypeDropDownList.DataBind();
            CommercialTypeDropDownList.Items.Insert(0, new ListItem() { Text = "-- Please select --", Value = "0" });

            TypeDropdown.DataSource = menuCommercial;
            TypeDropdown.DataTextField = "Description";
            TypeDropdown.DataValueField = "LookUpID";
            TypeDropdown.DataBind();
            TypeDropdown.Items.Insert(0, new ListItem() { Text = "-- Please select --", Value = "0" });
        }
    }



    private void ShowAddEditCommercial(string cid)
    {
        SilverlightService silverlightService = new SilverlightService();

        //DateTime commercialDate = DateTime.Today;
        //if (DateTime.TryParse(cid, out commercialDate))
        if (cid.ToInt32Safe() > 0)
        {

            var commercial = silverlightService.GetCommercial(cid.ToInt32Safe());
            if (commercial != null)
            {
                CommercialNameTextBox.Text = commercial.Name;
                CommercialTypeDropDownList.SelectedValue = commercial.TypeId.ToString();
                CommercialTypeDropDownList.Enabled = false;
                CommercialCost.Text = commercial.Cost.ToString();
                ScriptTextBox.Text = commercial.Script;
            }

            EditCommercialHeading.Visible = true;
            AddCommercialHeading.Visible = false;
        }
        else
        {

            EditCommercialHeading.Visible = false;
            AddCommercialHeading.Visible = true;
        }

        // first show add/edit commercial panel and hide list panel
        AddEditCommercialPanel.Visible = true;
        CommercialsListPanel.Visible = false;
    }

    private void BindCommercialList()
    {
        BindCommercialList(null, 0);
    }
    private void BindCommercialList(String name, int typeId)
    {
        SilverlightService silverlightService = new SilverlightService();
        var commercials = silverlightService.SearchCommercials(name, typeId);
        if (commercials != null && commercials.Count > 0)
        {
            CommercialsList.Visible = true;
            CommercialsList.DataSource = commercials;
            CommercialsList.DataBind();

            NoCommercialsMessagePanel.Visible = false;
        }
        else
        {
            CommercialsList.Visible = false;
            NoCommercialsMessagePanel.Visible = true;
        }

        // hide add/edit commercial panel and hide list panel
        AddEditCommercialPanel.Visible = false;
        CommercialsListPanel.Visible = true;
    }


    protected void SearchButton_Click(object sender, EventArgs e)
    {
        BindCommercialList(NameTextBox.Text, TypeDropdown.SelectedItem.Value.ToInt32Safe());
    }

    protected void SaveCommercialButton_Click(object sender, EventArgs e)
    {
        string cid = Request.QueryString["cid"];
        SilverlightService silverlightService = new SilverlightService();
        Commercial commercial = new Commercial();

        commercial.CommercialId = cid.ToInt32Safe();
        commercial.Name = CommercialNameTextBox.Text;
        commercial.TypeId = CommercialTypeDropDownList.SelectedItem.Value.ToInt32Safe();
        commercial.Cost = CommercialCost.Text.ToInt32Safe();
        commercial.Script = ScriptTextBox.Text;
        commercial.CreatedBy = "1";
        commercial.CreatedDate = DatetimeUtility.GetCurrentDateTime();

        if (commercial.CommercialId == 0)
        {
            NewsRepository news = new NewsRepository();
            var commercial1 = news.GetCommercial(CommercialNameTextBox.Text);
            if (commercial1 != null)
            {
                CommercialsSavedSuccessfullyPanel.Visible = false;
                CommercialsSaveErrorPanel.Visible = true;
                
                return;
            }
        }


        var result = silverlightService.SaveCommercial(commercial);
        if (result > 0)
        {
            CommercialsSavedSuccessfullyPanel.Visible = true;
            CommercialsSaveErrorPanel.Visible = false;
        }
        else
        {
            CommercialsSavedSuccessfullyPanel.Visible = false;
            CommercialsSaveErrorPanel.Visible = true;
        }
    }
}